package com.zjweu.hkg.Servies.impl;

import com.zjweu.hkg.DAO.HydroDAO;
import com.zjweu.hkg.DAO.HydroRelationDAO;
import com.zjweu.hkg.DAO.HydroMysql;

import com.zjweu.hkg.DTO.NodeRequest;
import com.zjweu.hkg.POJO.HydroNode;
import com.zjweu.hkg.POJO.HydroNodeRelation;
import com.zjweu.hkg.POJO.River;
import com.zjweu.hkg.Servies.HydroNodeService;
import com.zjweu.hkg.WebSocket.CounterWsServerEndPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class HydroNodeServiceImpl implements HydroNodeService {
    @Autowired
    HydroDAO hydroDAO;
    @Autowired
    HydroRelationDAO hydroRelationDAO;
    @Autowired
    HydroMysql hydroMysql;
    @Autowired
    CounterWsServerEndPoint counterWsServerEndPoint;

    @Override
    public int addNodeByName(String name) {
        HydroNode hydroNode=new HydroNode(name);
        hydroDAO.save(hydroNode);
        return 1;
    }

    /**
     *
     * @param nodeRequest
     * @return 0:成功创建
     */
    @Override
    public int addNodesAndRelation(NodeRequest nodeRequest) {
        //建立节点关系
        HydroNode hydroNode1=new HydroNode(nodeRequest.getNode1());
        HydroNode hydroNode2=new HydroNode(nodeRequest.getNode2());
        HydroNode hasNode1=hydroDAO.findHydroNodeByName(hydroNode1.getName());
        HydroNode hasNode2=hydroDAO.findHydroNodeByName(hydroNode2.getName());
        if(hasNode1!=null&&hasNode2!=null){
            hydroNode1=hasNode1;
            hydroNode2=hasNode2;
        }
        else if(hasNode1==null&&hasNode2!=null){
            hydroDAO.save(hydroNode1);
            hydroNode2=hasNode2;
        }
        else if(hasNode1!=null&&hasNode2==null){
            hydroDAO.save(hydroNode2);
            hydroNode1=hasNode1;
        }
        else{
            hydroDAO.save(hydroNode1);
            hydroDAO.save(hydroNode2);
        }

        HydroNodeRelation hydroNodeRelation=new HydroNodeRelation(hydroNode1,hydroNode2,nodeRequest.getRelation());

        hydroRelationDAO.save(hydroNodeRelation);
        return 0;
    }
    @Override
    public void clearAllNodes() {
        hydroDAO.clearAllNodes();
    }

    @Override
    public HydroNodeRelation[] getAll() {
        return hydroRelationDAO.getAll();
    }




    @Override
    public int addNodesAndRelationByList(List<NodeRequest> nodeRequestList){

        for (int i = 0; i < nodeRequestList.size(); i++) {
           addNodesAndRelation(nodeRequestList.get(i));
        }
        return 0;
    }

    @Override
    public int applyChangesToMysqlAndNeo4j(List<River> riverList) throws IOException {
        hydroMysql.deleteRiver();
        clearAllNodes();
        if(riverList.size()==0){
            return 1;
        }
        hydroMysql.insertRiverByList(riverList);
        createRelationFromRiver();
        return 0;
    }

    @Override
    public void createRelationFromRiver() throws IOException {
        //先删除所有节点和关系
        clearAllNodes();
        /**
         * name-from
         * name-to
         * name-level
         * name-riverBasin
         * riverBasin-flowTown
         * flowTown-landforms
         *
         */
        List<River> riverList=hydroMysql.findALLRiverFromMysql();
        List<NodeRequest> nodeRequestList=new ArrayList<>();
        for (int i = 0; i < riverList.size(); i++) {
            River river = riverList.get(i);
            nodeRequestList.add(new NodeRequest(river.getName(), river.getFrom(), "起点"));
            nodeRequestList.add(new NodeRequest(river.getName(), river.getTo(), "终点"));
            nodeRequestList.add(new NodeRequest(river.getName(), river.getLevel(), "等级"));
            nodeRequestList.add(new NodeRequest(river.getName(), river.getRiverBasin(), "流域"));
            nodeRequestList.add(new NodeRequest(river.getRiverBasin(), river.getFlowTown(), "流经"));
            nodeRequestList.add(new NodeRequest(river.getFlowTown(), river.getLandforms(), "属于"));
            // 正确计算进度百分比，保留整数
            int process = (int) Math.round((double) (i + 1) / riverList.size() * 100);
            System.out.println("process:" + process);
            counterWsServerEndPoint.sendMessage(String.valueOf(process));
        }
//        counterWsServerEndPoint.sendMessage("100.0");
        addNodesAndRelationByList(nodeRequestList);
    }
    @Override
    public List<River> getAllRiverFromMysql(){
       return hydroMysql.findALLRiverFromMysql();
    }
}
